import tpl from './list.tpl';
import './list.scss';

import { tplReplace } from '../../../utils/tools';
import $ from 'jquery';

export default _ => {

    let prize = null,       // 中奖下标
        activeIndex = null,      // 高亮标识
        Alltime = null,     
        gather = null,      // 存储dom
        _obj = [],           // 存储数据
        coordinate = [0,1,2,5,-1,8,7,6,3];      // 滚动顺序

    return {
        name: 'list',
        tpl( obj ) {
            let list = '';
            obj.forEach( item => {
                list += tplReplace( tpl, {
                    id: item.id,
                    name: item.text
                })
            })
            _obj = obj;
            return list;
        },

        run( vm, num ) {
            if ( num != 'draw_77' ) return;
            setTimeout( _ => {
                prize = Math.floor( Math.random() * 8 );
            },1000);
            // 存储li的dom
            gather = vm.find('li')
            this.lottery( 10 );
        },

        lottery( time ) {
            if ( time < 600 ) {
                this.dilution( time, 0.1 );
                return false;
            }
            
            if ( activeIndex != prize ) {
                this.dilution( time, 0.05 );
            } else {
                setTimeout( _ => {
                    console.log( activeIndex );
                    const tips = coordinate[activeIndex] == -1 ? 8 : coordinate[activeIndex];
                    alert(`恭喜抽中${_obj[tips].text}`);
                    Alltime = null;
                },10);
            }

        },

        dilution( time, second ) {
            Alltime = setTimeout( _ => {
                let ask = activeIndex + 1;
                activeIndex = ask % 9;
                gather.eq(coordinate[activeIndex])
                        .addClass('active')
                        .siblings('.active')
                        .removeClass('active');
                this.lottery( time + time * second );
            }, time )
        }

    }
}